/*
 * Utils.cc
 *
 *  Created on: Apr 2, 2011
 *      Author: howan
 */
#include <string>
#include <vector>
#include <functional>
#include <cassert>
#include <cstdlib>

#include "Utils.h"

namespace utils
    {

    void splitString(const std::string& str, char seperator, std::vector<std::string> & vec)
        {
        std::string::size_type i = 0;
        std::string::size_type j = str.find(seperator);

        while (j != std::string::npos)
            {
            vec.push_back(str.substr(i, j - i));
            i = ++j;
            j = str.find(seperator, j);

            if (j == std::string::npos)
                vec.push_back(str.substr(i, str.length()));
            }
        }

    IntIp_t strIp2int(const std::string & strIp)
        {
        IntIp_t intIp = 0;

        std::vector<std::string> strIpParts;
        splitString( strIp, '.', strIpParts);
        assert( strIpParts.size() == 4 );
        std::vector<std::string>::iterator pos;
        for ( pos=strIpParts.begin(); pos != strIpParts.end(); pos++)
            {
            IntIp_t ipPart = strtoul(pos->c_str(),NULL,0);
            intIp <<= 8;
            intIp += ipPart;
            }

        return intIp;
        }

    }
